//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.xjrsoft.module.bi.common.tool;

import java.nio.charset.Charset;
import java.util.Base64;

public class Base64Util {
    public static final Base64.Encoder ENCODER = Base64.getEncoder();
    public static final Base64.Encoder URL_ENCODER = Base64.getUrlEncoder();
    public static final Base64.Decoder DECODER = Base64.getDecoder();
    public static final Base64.Decoder URL_DECODER = Base64.getUrlDecoder();

    public Base64Util() {
    }

    public static String encode(String value) {
        return encode(value, Charsets.UTF_8);
    }

    public static String encode(String value, Charset charset) {
        byte[] val = value.getBytes(charset);
        return new String(encode(val), charset);
    }

    public static String encodeUrlSafe(String value) {
        return encodeUrlSafe(value, Charsets.UTF_8);
    }

    public static String encodeUrlSafe(String value, Charset charset) {
        byte[] val = value.getBytes(charset);
        return new String(encodeUrlSafe(val), charset);
    }

    public static String decode(String value) {
        return decode(value, Charsets.UTF_8);
    }

    public static String decode(String value, Charset charset) {
        byte[] val = value.getBytes(charset);
        byte[] decodedValue = decode(val);
        return new String(decodedValue, charset);
    }

    public static String decodeUrlSafe(String value) {
        return decodeUrlSafe(value, Charsets.UTF_8);
    }

    public static String decodeUrlSafe(String value, Charset charset) {
        byte[] val = value.getBytes(charset);
        byte[] decodedValue = decodeUrlSafe(val);
        return new String(decodedValue, charset);
    }

    public static byte[] encode(byte[] src) {
        return src.length == 0 ? src : ENCODER.encode(src);
    }

    public static byte[] decode(byte[] src) {
        return src.length == 0 ? src : DECODER.decode(src);
    }

    public static byte[] encodeUrlSafe(byte[] src) {
        return src.length == 0 ? src : URL_ENCODER.encode(src);
    }

    public static byte[] decodeUrlSafe(byte[] src) {
        return src.length == 0 ? src : URL_DECODER.decode(src);
    }

    public static String encodeToString(byte[] src) {
        return src.length == 0 ? "" : new String(encode(src), Charsets.UTF_8);
    }

    public static byte[] decodeFromString(String src) {
        return src.isEmpty() ? new byte[0] : decode(src.getBytes(Charsets.UTF_8));
    }

    public static String encodeToUrlSafeString(byte[] src) {
        return new String(encodeUrlSafe(src), Charsets.UTF_8);
    }

    public static byte[] decodeFromUrlSafeString(String src) {
        return decodeUrlSafe(src.getBytes(Charsets.UTF_8));
    }
}
